﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;
namespace GUI
{
    public partial class frmMatHang : Form
    {
        public frmMatHang()
        {
            InitializeComponent();
        }
        int sttkh;

        MatHangBUS mh = new MatHangBUS();

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

            //mh.insert();
        }

        private void frmMatHang_Load(object sender, EventArgs e)
        {
            tailaiDGV();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            MatHangDTO h = new MatHangDTO();
            XuatXuBUS xxbus = new XuatXuBUS();
            DonViTinhBUS dvbus = new DonViTinhBUS();
            h.MaMatHang = mh.LayMaMatHang() + 1;
            h.TenMatHang = txtTenMatHang.Text.Trim();
            h.MaXuatXu = xxbus.hai_LayMaXuatXuTheoTen(txtXuatXuMH.Text.Trim());
            h.MaDonViTinh = dvbus.LayMaDonViTinh(txtDonViTinhMH.Text.Trim());
            h.SoLuong = int.Parse(txtSoLuongTonMH.Text.Trim());
            h.GiaNhap = double.Parse(txtGiaNhapMH.Text.Trim());
            mh.insert(h);
            tailaiDGV();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            mh.delete(int.Parse(txtMaMatHang.Text.Trim()));
            tailaiDGV();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            MatHangDTO h = new MatHangDTO();
            XuatXuBUS xxbus = new XuatXuBUS();
            DonViTinhBUS dvbus = new DonViTinhBUS();
            h.MaMatHang = mh.LayMaMatHang() + 1;
            h.TenMatHang = txtTenMatHang.Text.Trim();
            h.MaXuatXu = xxbus.hai_LayMaXuatXuTheoTen(txtXuatXuMH.Text.Trim());
            h.MaDonViTinh = dvbus.LayMaDonViTinh(txtDonViTinhMH.Text.Trim());
            h.SoLuong = int.Parse(txtSoLuongTonMH.Text.Trim());
            h.GiaNhap = double.Parse(txtGiaNhapMH.Text.Trim());
            mh.update(h);
            tailaiDGV();
        }

        private void btnLàmMoi_Click(object sender, EventArgs e)
        {
            txtMaMatHang.Clear();
            txtTenMatHang.Clear();
            txtXuatXuMH.Clear();
            txtSoLuongTonMH.Clear();
            txtGiaNhapMH.Clear();
            txtDonViTinhMH.Clear();
            txtGhiChuMH.Clear();
        }
        private void tailaiDGV()
        {
            sttkh = 0;
            DataTable dt = mh.DataMatHang();
            XuatXuBUS xxbus = new XuatXuBUS();
            DonViTinhBUS dvbus = new DonViTinhBUS();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = dt.Rows[i];
                sttkh++;
                string tenxx = xxbus.hai_LayTenXuatXuTheoMa(int.Parse(dr["MaXuatXu"].ToString()));
                string tendv = dvbus.LayTenDonViTinh(int.Parse(dr["MaDonViTinh"].ToString()));
                string ghichu = "";
                if (dr["GhiChu"] != null)
                    ghichu = dr["GhiChu"].ToString();
                dataGridViewMatHang.Rows.Add(sttkh, dr["MaMatHang"].ToString(), dr["TenMatHang"].ToString(), tenxx, tendv, dr["SoLuong"].ToString(), dr["GiaNhap"].ToString(), ghichu);
            }
        }


        private void dataGridViewMatHang_SelectionChanged(object sender, EventArgs e)
        {
            int dong = dataGridViewMatHang.SelectedCells.Count;
            if (dong == 0)
                return;
            int bc = dataGridViewMatHang.SelectedCells[0].RowIndex;
            txtMaMatHang.Text = dataGridViewMatHang.Rows[bc].Cells[1].Value.ToString();
            txtTenMatHang.Text = dataGridViewMatHang.Rows[bc].Cells[2].Value.ToString();
            txtXuatXuMH.Text = dataGridViewMatHang.Rows[bc].Cells[3].Value.ToString();
            txtDonViTinhMH.Text = dataGridViewMatHang.Rows[bc].Cells[4].Value.ToString();
            txtSoLuongTonMH.Text = dataGridViewMatHang.Rows[bc].Cells[5].Value.ToString();
            txtGiaNhapMH.Text = dataGridViewMatHang.Rows[bc].Cells[6].Value.ToString();
            txtGhiChuMH.Text = dataGridViewMatHang.Rows[bc].Cells[7].Value.ToString();
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            //DialogResult dlr = MessageBox.Show("Bạn muốn thoát khỏi chương trình?", "Warnning", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            //if (dlr == DialogResult.Yes)
            //    this.Close();
            Close();
        }
    }
}
